package com.qdsgjsfk.vision.service;

import android.app.Service;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothGatt;
import android.bluetooth.BluetoothGattCallback;
import android.bluetooth.BluetoothGattCharacteristic;
import android.bluetooth.BluetoothGattService;
import android.bluetooth.BluetoothProfile;
import android.bluetooth.le.BluetoothLeScanner;
import android.bluetooth.le.ScanCallback;
import android.content.Context;
import android.content.Intent;
import android.os.Binder;
import android.os.Build;
import android.os.IBinder;
import android.util.Log;
import android.widget.Toast;

import com.qdsgjsfk.vision.util.BleBlueToothUtils;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.UUID;

import com.qdsgjsfk.vision.eventbus.Clear;
import com.qdsgjsfk.vision.interfac.BleResultCallBack;

/**
 * Created by machunyan on 2019/4/14.
 */
public class BleBlueToothService extends Service {

    private BluetoothAdapter bluetoothAdapter;
    private BluetoothGatt bluetoothGatt;
    private BluetoothLeScanner bluetoothLeScanner;
    private BluetoothGattCallback mBluetoothGattCallback;
    private BleResultCallBack bleResultCallBack;
    private BluetoothGattService gattService;
    private BluetoothGattCharacteristic gattCharacteristic;
    String s = "";

    private byte[][] data = new byte[][]{{2, 0, 19, 67, 79, 49, 50, 51, 52, 53, 54, 55, 56, 1, 73, -33, 77, -19, -61, -1},
            {41, -45, -26, 3}};
    private byte[] data2 = new byte[]{2, 0, 19, 67, 79, 49, 50, 51, 52, 53, 54, 55, 56, 1, 73, -33, 77, -19, -61, -1, 41, -45, -26, 3};

    private int indexTpye = 0;

    @Override
    public void onCreate() {
        super.onCreate();
        EventBus.getDefault().register( this );
        //获取蓝牙适配器
        bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
        bluetoothLeScanner = bluetoothAdapter.getBluetoothLeScanner();
        mBluetoothGattCallback = new BluetoothGattCallback() {

            //当连接状态发生改变
            @Override
            public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {
                super.onConnectionStateChange( gatt, status, newState );
                if (newState == BluetoothProfile.STATE_CONNECTED) {
                    Log.e( "STATE_CONNECTED--", "连接成功..." + gatt.getDevice().getName() );
                    gatt.discoverServices();
                    bleResultCallBack.onDiscoverServicesSuccess();
                } else if (newState == BluetoothProfile.STATE_DISCONNECTED) {
                    Log.e( "mcy", "连接断开..." );
                    bleResultCallBack.onDiscoverServicesFail();
                } else if (newState == BluetoothProfile.STATE_DISCONNECTING) {
                    Log.e( "mcy", "连接ing..." );
                }
            }

            //发现新服务，即调用了mBluetoothGatt.discoverServices()后，返回的数据
            @Override
            public void onServicesDiscovered(BluetoothGatt gatt, int status) {
                super.onServicesDiscovered( gatt, status );
                if (status == BluetoothGatt.GATT_SUCCESS) {
                    Log.e( "mcy", "发现服务成功..." );
                    gattService = gatt.getService( UUID.fromString( "49535343-fe7d-4ae5-8fa9-9fafd205e455" ) );
                    indexTpye = 1;
                    if (gattService == null) {
                        indexTpye = 2;
                        gattService = gatt.getService( UUID.fromString( "0000ffe0-0000-1000-8000-00805f9b34fb" ) );
                    }
                    if (gattService == null) {
                        Log.e( "mcy", "获取bluetoothGattService失败..." );
                    } else {
                        if (indexTpye == 1) {
                            gattCharacteristic = gattService.getCharacteristic( UUID.fromString( "49535343-8841-43F4-A8D4-ECBE34729BB3" ) );
                        } else {
                            gattCharacteristic = gattService.getCharacteristic( UUID.fromString( "0000ffe1-0000-1000-8000-00805f9b34fb" ) );
                        }
                        if (gattCharacteristic == null) {
                            Log.e( "mcy", "获取Characteristic失败..." );

                        } else {
                            bluetoothGatt.setCharacteristicNotification( gattCharacteristic, true );//这一句是为了接受蓝牙数据,必须写!!!否则接受不到数据

                        }
                    }
                } else {
                    Log.e( "mcy", "发现服务失败..." );

                }

            }

            //读取从设备传递过来的数据值，在这里读数据
            @Override
            public void onCharacteristicRead(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) {
                super.onCharacteristicRead( gatt, characteristic, status );
            }

            //发送数据后的回调
            @Override
            public void onCharacteristicWrite(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) {
                super.onCharacteristicWrite( gatt, characteristic, status );
                Log.e( "mcy", "onCharacteristicWrite...发送成功后走这个方法" );

            }

            @Override
            public void onCharacteristicChanged(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic) {
                super.onCharacteristicChanged( gatt, characteristic );
                Log.e( "onCharacteristicChanged", "---蓝牙回传数据onCharacteristicChanged---" );
                byte[] value = characteristic.getValue();

                Log.e( "value", value.length + "" );
                if (value.length == 20) {
                    s = s + bytesToString( value, 20 );
                    if (BleBlueToothUtils.getJSONType( s )) {
                        //能解析成json
                        bleResultCallBack.onReturnResult( s );
                    }
                } else {
                    s = s + bytesToString( value, value.length );
                    bleResultCallBack.onReturnResult( s );
                }


            }

        };
    }


    public class BleBlueToothBind extends Binder {

        public BluetoothAdapter getAdapter() {
            return bluetoothAdapter;
        }

        public void setBleResultCallBack(BleResultCallBack bleResultCallBack) {
            BleBlueToothService.this.bleResultCallBack = bleResultCallBack;
        }

        /**
         * 扫描
         */
        public void scanLeDevice(final BluetoothAdapter.LeScanCallback leScanCallback, final ScanCallback scanCallback) {

            if (Build.VERSION.SDK_INT > Build.VERSION_CODES.LOLLIPOP) {
                if (bluetoothAdapter.isEnabled() && bluetoothLeScanner != null) {
                    bluetoothLeScanner.startScan( scanCallback );//开始搜索
                } else {
                    Log.e( "mcy", "蓝牙不可用..." );
                }
            } else {
                if (bluetoothAdapter.isEnabled()) {
                    bluetoothAdapter.startLeScan( leScanCallback ); //开始搜索
                } else {
                    Log.e( "mcy", "蓝牙不可用..." );
                }
            }
            Log.e( "mcy", "开始扫描..." );
        }

        /**
         * 停止扫描
         */
        public void stopScan(BluetoothAdapter.LeScanCallback mLeScanCallback, ScanCallback scanCallback) {
            Log.e( "mcy", "停止扫描..." );
            if (bluetoothAdapter != null && mLeScanCallback != null) {
                bluetoothAdapter.stopLeScan( mLeScanCallback );
            }
            if (bluetoothLeScanner != null && scanCallback != null) {
                bluetoothLeScanner.stopScan( scanCallback );
            }

        }


        /**
         * 连接方式二
         */

        public void connectLeDevice(Context context, BluetoothDevice device) {
            bluetoothGatt = device.connectGatt( context, false, mBluetoothGattCallback );
        }

        /**
         * 连接方式一
         */
        public void connection(Context context, String address) {
            if (BluetoothAdapter.checkBluetoothAddress( address )) {
                BluetoothDevice remoteDevice = bluetoothAdapter.getRemoteDevice( address );
                if (remoteDevice == null) {
                    Log.e( "mcy", "设备不可用" );
                }
                connectLeDevice( context, remoteDevice );
            } else {
                Log.e( "mcy", "设备不可用" );
            }
        }


        /**
         * 断开连接
         */
        public void cancleConnection() {
            if (bluetoothGatt != null) {
                bluetoothGatt.close();
                Log.e( "mcy", "主动断开连接..." );
            }
        }

    }


    @Override
    public IBinder onBind(Intent intent) {
        return new BleBlueToothBind();
    }

    public static String bytesToString(byte[] b, int length) {
        StringBuffer result = new StringBuffer( "" );
        for (int i = 0; i < length; i++) {
            result.append( (char) (b[i]) );
        }

        return result.toString();
    }

    @Subscribe(threadMode = ThreadMode.MAIN, sticky = true)
    public void onMessageEvent(Clear clear) {
        Log.e( "EventBusBle---", "true" );
        Toast.makeText( this, "qingchu", Toast.LENGTH_SHORT );
        if (clear.clear == 1) {
            s = "";
        }
    }

    @Override
    public void onDestroy() {
//        EventBus.getDefault().unregister( this );
        Log.e( "BleonDestroy---", "onDestroy" );
        super.onDestroy();
    }

}
